package gui;

import entities.Student;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import commands.CreatePaymentPlan;
import commands.LoginCheckCommand;

/**
 * 
 * @author eks691
 * 
 */
public class EmployeeAllStudentInfoPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * Student the panel is displaying
	 */
	Student student;

	/** id of either the student, manager, or employee using the system */
	private int id;

	public EmployeeAllStudentInfoPanel(Student s, int i) {
		student = s;
		System.out.println("New class; Name of Student: " + s.getName());
		id = i;

		// setLayout(new BorderLayout());
		// panels are set out horizontally
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(new TitledBorder(new EtchedBorder(), "Student Information"));
		EmployeeStudentInfoPanel sInfoPanel = new EmployeeStudentInfoPanel(
				student);
		add(sInfoPanel);
		EmployeeKinInfoPanel kInfoPanel = new EmployeeKinInfoPanel(student);
		add(kInfoPanel);
		EmployeeFinInfoPanel fInfoPanel = new EmployeeFinInfoPanel(student);
		add(fInfoPanel);
		EmployeeLoanInfoPanel lInfoPanel = new EmployeeLoanInfoPanel(student);
		add(lInfoPanel);
		if (student.getPaymentPlanCreated()) {
			PaymentInfoPanel pInfoPanel = new PaymentInfoPanel();
			add(pInfoPanel);
		}
		ButtonPanel bPanel = new ButtonPanel();
		add(bPanel);
	}

	/**
	 * Construct a panel for the payment information of the student
	 */
	private class PaymentInfoPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private PaymentInfoPanel() {
			NumberFormat decFormat = new DecimalFormat("#0.00");

			setBorder(new TitledBorder(new EtchedBorder(),
					"Payment Information"));
			setLayout(new GridLayout(5, 2));

			JLabel amountPaid = new JLabel("Amount Paid-Out: $"
					+ decFormat.format(student.getPaymentPlan()
							.getAmountReceived()));
			add(amountPaid);
			JLabel blank = new JLabel("");
			add(blank);
			JLabel nextPaymentTitle = new JLabel("Next Payment: ");
			add(nextPaymentTitle);
			JLabel previousPaymentTitle = new JLabel("Previous Payment: ");
			add(previousPaymentTitle);
			JLabel nextPaymentDate = new JLabel("   Date: "
					+ student.getPaymentPlan().getNextDate());
			add(nextPaymentDate);
			JLabel previousPaymentDate = new JLabel("   Date: "
					+ student.getPaymentPlan().getPrevDate());
			add(previousPaymentDate);
			JLabel nextPaymentAmount = new JLabel(
					"   Amount: $"
							+ decFormat.format(student.getPaymentPlan()
									.getNextAmount()));
			add(nextPaymentAmount);
			JLabel previousPaymentAmount = new JLabel(
					"   Amount: $"
							+ decFormat.format(student.getPaymentPlan()
									.getPrevAmount()));
			add(previousPaymentAmount);
		}
	}

	/**
	 * Construct a panel for the buttons for the panel
	 */
	private class ButtonPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private ButtonPanel() {
			setLayout(new GridLayout(1, 4, 50, 50));

			JButton changeInfo = new JButton("Update Student Information");
			add(changeInfo);

			JButton viewCosigner = new JButton("View Cosigner");
			add(viewCosigner);
			JButton createPlan = new JButton("Create Payment Plan");
			if (student.getLoan().isApproved() == true
					&& student.getPaymentPlanCreated() == false) {
				add(createPlan);
			}
			JButton back = new JButton("Back");
			add(back);

			// add listeners for the buttons
			createPlan.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					// create plan
					new CreatePaymentPlan(student.getID());
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeAllStudentInfoPanel newPanel = new EmployeeAllStudentInfoPanel(
							student, id);
					frame.changePanel(newPanel);
				}
			});

			viewCosigner.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeCosignerInfoPanel newPanel = new EmployeeCosignerInfoPanel(
							student.getID(), id);
					frame.changePanel(newPanel);
				}
			});

			back.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeAllStudentPanel newPanel = new EmployeeAllStudentPanel(
							id);
					frame.changePanel(newPanel);
				}
			});

			changeInfo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame myFrame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					myFrame.setVisible(true);
					EmployeeChangeStudentInfoPanel newPanel = new EmployeeChangeStudentInfoPanel(
							student.getID(), id);
					myFrame.changePanel(newPanel);
				}
			});

		}
	}
}
